import { BillApi } from '@/api/Bill.api.js'
import { Common } from '@/utils/common.js'
import config from '@/config'
const baseUrl = process.env.NODE_ENV === 'development' ? config.baseUrl.dev : config.baseUrl.pro.replace('/api', '/')

export default {
  name: '',

  data () {
    return {
      isShowTableFuwufeihuizongModal: false,
      companyName: null,
      companyId: null,
      year: null,
      month: null,
      billDate: null,
      tableData: [],
      newTableData: [],
      tableComone: {
        pageIndex: 1, // 页码
        totalRows: 0, // 总条数
        pageSize: 10 // 当前页面展示条数
      },
      handelBills: null
    }
  },

  watch: {
  },

  // mounted () {
  //   this.companyId = this.$route.query.companyId
  // },

  methods: {
    /**
     * 分页页码变动
     * @param {Number} val pageIndex
     */
    onChangePage (val) {
      this.tableComone.pageIndex = val
      this.query()
    },
    /**
     * 分页页码变动
     * @param {Number} val pageSize
     */
    onpageSizeChange (val) {
      this.tableComone.pageIndex = 1
      this.tableComone.pageSize = val
      this.query()
    },
    /**
     * 分页查询接口
     * @param {Object} obj {pageIndex, pageSize}
     */
    async query () {
      this.leftNavArr = ['人数']
      var params = {
        CustomerIds: this.companyId,
        // year: this.year,
        month: this.billDate
      }
      let res = await BillApi.getMergedSFTotal(params)
      console.log('服务费汇总报表res: ', res)
      if (res.status === 200 && res.data.success) {
        var tempTableData = []
        this.tableComone.totalRows = res.data.totalRows
        this.companyName = res.data.data.companyName

        var tempEmployeeCount = {} // 用于保存各项服务费的人数，最终会赋值给表格第一行
        var tempCompanyTotalAmt = {} // 用于保存各项服务费的合计，最终会赋值给表格第二行
        res.data.data.insures.map(item => { // 循环解析insureItems里面的companyFee、employeeFee值并赋到相应的key
          var itemSumStr = item.category + 'companyTotalAmt' // 每条item添加一个属性，对应表格里面的key
          tempEmployeeCount = { ...tempEmployeeCount, ...{ [itemSumStr]: item.employeeCount } }
          tempCompanyTotalAmt = { ...tempCompanyTotalAmt, ...{ [itemSumStr]: item.companyTotalAmt } }
        })
        tempTableData[0] = tempEmployeeCount
        // tempTableData[1] = tempCompanyTotalAmt
        this.newTableData = tempTableData

        if (res.data.data.handelBills) {
          this.handelBills = res.data.data.handelBills
          var handelBills = res.data.data.handelBills
          handelBills.map(item => {
            item.year = this.year
            item.month = this.month
            item.billFees.map(c_item => {
              var str = c_item.isPersonalBurden ? c_item.category + 'companyFeeamt' : c_item.category + 'companyTotalAmt'
              item[str] = c_item.amt
            })
            this.leftNavArr.push(item.type)
            console.log('item: ', item)
            item.employeeName = item.type
            this.newTableData.push(item)
          })
          this.leftNavArr.push('合计')
          this.newTableData.push(tempCompanyTotalAmt)
          console.log('handelBills: ', handelBills)
          console.log('this.leftNavArr: ', this.leftNavArr)
        }
      } else {
        this.newTableData = []
        this.$Message.error({
          content: res.data.message,
          duration: 10,
          closable: true
        })
      }
      this.loading = false
    },

    ok () {
    },

    cancel () {
    },

    handleExportData () {
    },

    parsingItems (row, item, oneTitle, twoTitle1, twoTitle2, type) {
      var sumTitle = oneTitle + twoTitle1 + type
      console.log('sumTitle: ', sumTitle)
      row[sumTitle] = item[twoTitle1][type]
    },

    // 导出
    async exportServiceFeeTotal () {
      var params = {
        CustomerIds: this.companyId,
        companyName: this.companyName,
        year: this.year,
        month: this.month,
        // month: this.billDate,
        insures: this.newTableData,
        handelBills: this.handelBills
      }
      console.log('导出params: ', params)
      let res = await BillApi.exportServiceFeeTotal(params)
      if (res.status === 200 && res.data.success) {
        console.log('❤ 返回链接: ', baseUrl + res.data.message)
        window.open(baseUrl + res.data.message, '_blank')
      } else {
        this.$Message.error({
          content: res.data.message,
          duration: 10,
          closable: true
        })
      }
    }
  }
}
